package com.example.controller;

import com.example.dto.ResultDTO;
import com.example.exception.CustomizeErrorCode;
import com.example.model.User;
import com.example.model.UserInfo;
import com.example.service.UploadService;
import com.example.service.impl.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Author by admin, Email xx@xx.com, Date on 2020/1/28.
 * PS: Not easy to write code, please indicate.
 */
@Controller
@RequestMapping("/userInfo")
@Slf4j
public class UserInfoController {


    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UploadService uploadService;


    @GetMapping("/queryInfo")
    public String queryInfo(Model model, HttpServletRequest request)
    {
        User user = (User) request.getSession().getAttribute("user");
        UserInfo userInfo = userInfoService.queryuserinfo(user);
        if (userInfo!=null){
            model.addAttribute("userInfo",userInfo);
        }
        return "userInfo";
    }
    @PostMapping("/updateInfo")
    @ResponseBody
    public ResultDTO updateInfo(@RequestBody UserInfo userInfoModel){
        userInfoModel.setGmtModified(new Date());
        boolean b = userInfoService.updateUser(userInfoModel);
        if (b)
        {
            return ResultDTO.okOf("更新成功！",userInfoModel);
        }
        return ResultDTO.errorOf(CustomizeErrorCode.UPDATE_ERROR);
    }

    @RequestMapping("/uploadImg")
    @ResponseBody
    public ResultDTO insert(@RequestParam("file") MultipartFile file) {
        return uploadService.avatarUpload(file);

    }

}
